//
//  ZJPictureView.swift
//  Webo
//
//  Created by 曾静 on 16/6/30.
//  Copyright © 2016年 qq. All rights reserved.
//

import UIKit
private let pictureId = "pictureId"
let itemMargin: CGFloat = 5

let  itemWH = (SCREENW - 2 * itemMargin - 2 * OriginalMargin)/3

class ZJPictureView: UICollectionView {
    
    var status:ZJStatues?{
        
        didSet{
            
             countLabel.text = "\(status?.pic_urls?.count ?? 0)"
            //为collection设置约束
            self.snp_updateConstraints { (make) -> Void in
                
                make.size.equalTo(status!.picSize!)
            }
           
            
            //设置完数据记得刷新UI
            
            reloadData()
            
        }
    }

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //设置视图,懒加载控
    private lazy var countLabel:UILabel = UILabel(textColor: UIColor.redColor(), textSize: 35)
    
    func setupUI(){
        
        dataSource = self
        
        backgroundColor = RGB(237, green: 237, blue: 237)
        
        registerClass(PictureCell.self, forCellWithReuseIdentifier: pictureId)
        
       let layout = collectionViewLayout as!UICollectionViewFlowLayout
        
        layout.itemSize = CGSizeMake(itemWH, itemWH)
        
        layout.minimumInteritemSpacing = itemMargin
        
        layout.minimumLineSpacing = itemMargin
        
        addSubview(countLabel)
        
        countLabel.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
        }
    }
    

}
extension ZJPictureView:UICollectionViewDataSource{
    
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return status?.pic_urls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureId, forIndexPath: indexPath) as!PictureCell
        
        cell.pictureInfo = status!.pic_urls![indexPath.item]
        
        return cell
    }
}



class PictureCell: UICollectionViewCell {
    
    //定义一个属性,恭外界赋值
    
    var pictureInfo:ZJPicture?{
        didSet{
            itemImageView.sd_setImageWithURL(NSURL(string: pictureInfo?.thumbnail_pic ?? ""), placeholderImage: UIImage(named: "timeline_card_top_background"))
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //设置视图
        setupUI()
    }
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        
        contentView.addSubview(itemImageView)
        
        itemImageView.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
    }
    
    //懒加载控件
    private lazy var itemImageView:UIImageView = {
        
        let imageView = UIImageView(imageName: "timeline_card_top_background")
        
        imageView.contentMode = UIViewContentMode.ScaleAspectFill
        
        imageView.clipsToBounds = true
        
        return imageView
        
    }()
    
    
    
    
}